package com.trt.controller;

import com.trt.base.BaseInfoProperties;
import com.trt.bo.CommentBO;
import com.trt.enums.MessageEnum;
import com.trt.grace.result.GraceJSONResult;
import com.trt.pojo.Comment;
import com.trt.pojo.Vlog;
import com.trt.service.CommentService;
import com.trt.service.MsgService;
import com.trt.service.VlogService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * @author tanrentao
 * @date 2021-11-18
 * @desc
 */
@Slf4j
@Api(tags = "CommentController 评论模块的接口")
@RequestMapping("comment")
@RestController
public class CommentController extends BaseInfoProperties {

    @Autowired
    private CommentService commentService;

    @Autowired
    private MsgService msgService;

    @Autowired
    private VlogService vlogService;

    @PostMapping("create")
    public GraceJSONResult create(@RequestBody @Valid CommentBO commentBO) {
        return GraceJSONResult.ok(commentService.createComment(commentBO));
    }

    @GetMapping("counts")
    private GraceJSONResult counts(@RequestParam String vlogId) {
        String countsStr = redisOperator.get(REDIS_VLOG_COMMENT_COUNTS + ":" + vlogId);
        if (StringUtils.isBlank(countsStr)) {
            countsStr = "0";
        }
        return GraceJSONResult.ok(Integer.valueOf(countsStr));
    }

    @GetMapping("list")
    public GraceJSONResult list(@RequestParam String vlogId,
                                @RequestParam(defaultValue = "") String userId,
                                @RequestParam Integer page,
                                @RequestParam Integer pageSize) {

        return GraceJSONResult.ok(commentService.queryVlogComments(vlogId, userId, page, pageSize));
    }

    @DeleteMapping("delete")
    public GraceJSONResult delete(@RequestParam String commentUserId,
                                  @RequestParam String commentId,
                                  @RequestParam String vlogId) {

        commentService.deleteComment(commentUserId, commentId, vlogId);
        return GraceJSONResult.ok();
    }

    @PostMapping("like")
    public GraceJSONResult like(@RequestParam String commentId,
                                @RequestParam String userId) {

        // Fixme 故意犯错，bigkey
        // 错误, 100万条，REDIS_VLOG_COMMENT_LIKED_COUNTS中有100万条id，这个key非常大,要避免
        // 修改1 要使用incrementHash的话  针对commentId再做一个hash分为10个或者100个，1000个方式做一个hash路由方式存储
        redisOperator.incrementHash(REDIS_VLOG_COMMENT_LIKED_COUNTS, commentId , 1);
        // 修改2 使用单体increment   把REDIS_VLOG_COMMENT_LIKED_COUNTS + ":" + commentId作为key，而不是把REDIS_VLOG_COMMENT_LIKED_COUNTS做对象做
        //redisOperator.increment(REDIS_VLOG_COMMENT_LIKED_COUNTS + ":" + commentId , 1);

        redisOperator.setHashValue(REDIS_USER_LIKE_COMMENT, userId + ":" + commentId , "1");
        // redisOperator.hset(REDIS_USER_LIKE_COMMENT, userId + ":" + commentId , "1"); 同上一样

        // 系统消息: 点赞评论
        Comment comment = commentService.getComment(commentId);
        Vlog vlog = vlogService.getVlog(comment.getVlogId());
        Map msgContent = new HashMap();
        msgContent.put("vlogId", vlog.getId()); // vlog.getId()
        msgContent.put("vlogCover", vlog.getCover());
        msgContent.put("commentId", commentId);
        msgService.createMsg(userId, comment.getCommentUserId(), MessageEnum.LIKE_COMMENT.type, msgContent);

        return GraceJSONResult.ok();
    }

    @PostMapping("unlike")
    public GraceJSONResult unlike(@RequestParam String commentId,
                                @RequestParam String userId) {

        redisOperator.decrementHash(REDIS_VLOG_COMMENT_LIKED_COUNTS, commentId , 1);
        redisOperator.hdel(REDIS_USER_LIKE_COMMENT, userId + ":" + commentId);

        return GraceJSONResult.ok();
    }





}
